//
//  Utility.m
//  DrawCircle
//
//  Created by Nguyen Tan Loc on 5/14/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Utility.h"
#import "DefineXML.h"
#import "AlphaLayer.h"
#import "SimpleAudioEngine.h"
#import "Setting.h"

@implementation Utility

static Utility *m_Instance = nil;

+(Utility *) getInstance
{
    if (m_Instance == nil)
        m_Instance = [[Utility alloc] init];
    return m_Instance;
}

-(id) init
{
    if (self = [super init])
    {
        
        [self loadAnimationInfo];
    }
    return self;
}

-(float) vectorToAngle:(CGPoint)vector
{
    return atan2f(vector.x, vector.y) * 180.0f / kmPI;
}

-(float) vectorToRadian:(CGPoint)vector
{
    return atan2f(vector.x, vector.y);
}

-(void) loadAnimationInfo
{
    mAnimationDictionary = [[NSMutableDictionary alloc] init];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:XML_ANIMATION ofType:@"xml"];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;
    
    mAnimationData = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    
    if (mAnimationData == nil)
    {
        // process here
        CCLOG(@"document = nil");
    }
    
    [xmlData release];
    xmlData = nil;
}

-(CCAction *) getAnimWithKey:(NSString *)key delay:(float)delay loop:(int)loop
{
    CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[self getListSpriteFrameWithKey:key] delay:delay];
    CCAnimate *animate = [CCAnimate actionWithAnimation:animation];
    
    if (loop <= 0)
    {
        CCRepeatForever *repeatForever = [CCRepeatForever actionWithAction:animate];
        return repeatForever;
    }
    else
    {
        CCRepeat *repeat = [CCRepeat actionWithAction:animate times:loop];
        return repeat;
    }
}

-(NSArray *) getListSpriteFrameWithKey:(NSString *)key
{
    NSMutableArray *listSpriteFrame = [NSMutableArray array];
    NSArray *listFrameName = [mAnimationDictionary objectForKey:key];
    
    if (listFrameName == nil)
    {
        listFrameName = [self loadListFrameNameWithKey:key];
        [mAnimationDictionary setObject:listFrameName forKey:key];
    }
    
    for (int i = 0; i < listFrameName.count; i++)
    {
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[listFrameName objectAtIndex:i]];
        [listSpriteFrame addObject:spriteFrame];
    }
    
    return listSpriteFrame;
}

-(NSArray *) loadListFrameNameWithKey:(NSString *)key
{
    NSMutableArray *listFrameName = [NSMutableArray array];
    NSArray *listAnimationInfo = [mAnimationData.rootElement elementsForName:key];
    for (GDataXMLElement *eAnimation in listAnimationInfo)
    {
        NSArray *listFrameNameInfo = [eAnimation elementsForName:XML_FRAME];
        for (GDataXMLElement *eFrameName in listFrameNameInfo)
        {
            NSString *frameName = [[eFrameName attributeForName:XML_NAME] stringValue];
            [listFrameName addObject:frameName];
        }
    }
    
    return listFrameName;
}

-(void) saveObject:(id<NSCoding>)object forKey:(NSString *)key
{
    NSData *archivedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:archivedObject forKey:key];
    [defaults synchronize];
}

-(id) loadObjectForKey:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *archivedObject = [defaults objectForKey:key];
    
    if (archivedObject == nil)
        return nil;
    return [NSKeyedUnarchiver unarchiveObjectWithData:archivedObject];
}

-(void) playEffect:(NSString *)str
{
    if ([Setting getInstance]._isSoundFX)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:str];
    }
}

@end

